
import java.net.URL;
import java.net.URLClassLoader;

/**
* 可以重新载入同名类的类加载器实现
*
* 放弃了双亲委派的加载链模式.
* 需要外部维护重载后的类的成员变量状态.
*
* @author ken.wu
* @mail ken.wug@gmail.com
* 2007-9-28 下午01:37:43
*/
public class HotswapClassLoader extends URLClassLoader {

    public HotswapClassLoader(URL[] urls) {
        super(urls);
    }

    public HotswapClassLoader(URL[] urls, ClassLoader parent) {
        super(urls, parent);
    }

    public Class<?> load(String name) throws ClassNotFoundException {
        return load(name, false);
    }

    public Class<?> load(String name, boolean resolve) throws ClassNotFoundException {
        if (null != super.findLoadedClass(name)) {
            return reload(name, resolve);
        }
        Class<?> clazz = super.findClass(name);

        if (resolve) {
            super.resolveClass(clazz);
        }
        return clazz;
    }

    @SuppressWarnings("resource")
    public Class<?> reload(String name, boolean resolve) throws ClassNotFoundException {
        return new HotswapClassLoader(super.getURLs(), super.getParent()).load(name, resolve);
    }

    //HotSwapClassLoader c1 = new HotSwapClassLoader( new URL[]{ new URL( "file:\e:\test\")} , 
    //      a.getClass().getClassLoader());  
    //Class clazz = c1.load(" test.hotswap.A ");  
}
